<?php
namespace api\modules\changyue\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use izyue\admin\models\Member;
use izyue\admin\models\MemberSearch;
use izyue\admin\models\DataOrder;
use izyue\admin\models\DataOrderSearch;
use linslin\yii2\curl;

/**
 * Site controller
 */
class SiteController extends Controller
{
    public $app_id = 'Gl3FPNKc3r';
    public $app_secret = 'IHOljUKaHopWmqDpHlai';
    public function init(){
        $this->enableCsrfValidation = false; //取消post csrf验证 否则无法post数据
    }

    public function actionSubmit()
    {

        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $post = Yii::$app->request->post();
        $sign   =   MD5($this->app_id.$this->app_secret);
        $url = 'http://api.23456g.com/api/v2/order/submit.json' ;
        $param = array(
            'app_id' => $this->app_id,
            'verify_sign' => $sign,
            'out_order_id' => $post['out_order_id'],
            'flow_val' => $post['flow_val'],
            'phone_no' => $post['phone_no'],
            'timestamp' => date('YmdHmsB',time()),
            'scope' => $post['scope']?$post['scope']:"0",
        );

        $return = $this->curl_http($url,$param);
        return json_decode($return,true);
    }
    //余额查询接口
    public function actionBlance()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $sign   =   MD5($this->app_id.$this->app_secret);
        $url = 'http://api.23456g.com/api/v1/account/queryblance.json' ;
        $param = array(
                    'app_id' => $this->app_id,
                    'verify_sign' => $sign
                    );
        $return = $this->curl_http($url,$param);
        return json_decode($return,true);
          
    }
    //订单查询接口
    public function actionQuery()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $post = Yii::$app->request->post();
        $sign   =   MD5($this->app_id.$this->app_secret);
        $url = 'http://api.23456g.com/api/v1/order/query.json' ;
        $param = array(
            'app_id' => $this->app_id,
            'verify_sign' => $sign,
            'out_order_id' => $post['out_order_id'],
        );
        $return = $this->curl_http($url,$param);
        $array = json_decode($return,true);
        return $array;
    }
    /*//回调地址接口
    public function actionBack()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;  
        $param = Yii::$app->request->post();
        $this->UpdateOrderStatus($param);
        $backurl = $this->GetBackUrl($param);
        $curl_array = array(
                    'order_id' => $param['order_id'], 
                    'out_order_id' => $param['out_order_id'], 
                    'status' => $param['status'],
                    'completion_time' => $param['completion_time'],
                    'err_desc' => $param['err_desc'],
                    );
        $return = $this->curl_http($backurl,$curl_array);
        //如果没有返回则定时回调
        return $return;  
    }*/

    public function UpdateOrderStatus($param) {
        $res = DataOrder::find()->where(['out_order_id' => $param['out_order_id']])->one();
        DataOrder::updateAll(
            array('status'=>$param['status']),
            'out_order_id=:out_order_id',
            array(':out_order_id'=>$res['out_order_id'])
            );
    }
    public function GetBackUrl($param){
        $res = DataOrder::find()->where(['out_order_id' => $param['out_order_id']])->one();
        $info = Member::findOne($res['mid']);
        return $info['back_url'];

    }
    public function curl_http($url,$param){
        $curl = new curl\Curl();
        $response = $curl->setOption(
                CURLOPT_POSTFIELDS, 
                http_build_query($param)
                )
            ->post($url);
        return $response;
    }


}
